<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>uCXpresso.BLE: /ble/ble_multiple/src/main.cpp</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../upnp.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">uCXpresso.BLE
   &#160;<span id="projectnumber">v1.0.4</span>
   </div>
   <div id="projectbrief">RTOS C++ Framework for Bluetooth Low Energy</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="../../index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="../../modules.html"><span>Modules</span></a></li>
      <li><a href="../../annotated.html"><span>Classes</span></a></li>
      <li><a href="../../examples.html"><span>Examples</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="../../search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="../../search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('d5/df3/_2ble_2ble_multiple_2src_2main_8cpp-example.html','../../');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Groups</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">/ble/ble_multiple/src/main.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>This is an example of how to use the <a class="el" href="../../d8/d3b/classble_battery_level.html">bleBatteryLevel</a> class. More details about this example.</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> ===============================================================================</span></div>
<div class="line"><span class="comment"> Name        : main.cpp</span></div>
<div class="line"><span class="comment"> Author      : Jason</span></div>
<div class="line"><span class="comment"> Version     : 1.0.5</span></div>
<div class="line"><span class="comment"> Date        : 2014/3/20</span></div>
<div class="line"><span class="comment"> Copyright   : Copyright (C) www.embeda.com.tw</span></div>
<div class="line"><span class="comment"> License     : MIT</span></div>
<div class="line"><span class="comment"> Description : a BLE Firmat &amp; multiple services Demo</span></div>
<div class="line"><span class="comment"> ===============================================================================</span></div>
<div class="line"><span class="comment"> History</span></div>
<div class="line"><span class="comment"> ---------+---------+--------------------------------------------+-------------</span></div>
<div class="line"><span class="comment"> DATE     | VERSION |   DESCRIPTIONS                             |  By</span></div>
<div class="line"><span class="comment"> ---------+---------+--------------------------------------------+-------------</span></div>
<div class="line"><span class="comment"> 2014/1/1   v1.0.0  Initialize                                      Jason</span></div>
<div class="line"><span class="comment"> 2014/1/20  v1.0.1  Add Battery Level and Health Thermometer demo   Jason</span></div>
<div class="line"><span class="comment"> 2014/2/22  v1.0.2  Add tone in myProximity class                   Jason</span></div>
<div class="line"><span class="comment"> 2014/2/28  v1.0.3  Add Device Information Service                  Jason</span></div>
<div class="line"><span class="comment"> 2014/3/2   v1.0.4  Add onWatchdog() event in myBLE class.          Jason</span></div>
<div class="line"><span class="comment">                    Add onError() event in myBLE class.</span></div>
<div class="line"><span class="comment"> 2014/3/20  v1.0.5  Add Servo Motor for BLE Arduino Firmata Demo.   Jason</span></div>
<div class="line"><span class="comment"> ===============================================================================</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &quot;uCXpresso.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/serial.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/usb_cdc.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#include &quot;debug.h&quot;</span></div>
<div class="line"><span class="preprocessor">#define DBG     dbg_printf</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define DBG(...)</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span><span class="comment">//</span></div>
<div class="line"><span class="comment">// TODO: insert other include files here</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/thread.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/bus.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/timeout.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/power.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &quot;class/ble_serial.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/ble_battery.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/ble_ht.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/ble_proximity.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;class/ble_devinfo.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &quot;MyFirmata.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;callback.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;arduino/tone.h&quot;</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// TODO: insert other definitions and declarations here</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"></div>
<div class="line"><a name="_a0"></a><a class="code" href="../../de/d89/class_c_bus.html">CBus</a> leds(<a name="a1"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fadac6477842247cab1a8c02c65f431b44">LED1</a>, <a name="a2"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fa8379bbaa96d151e6adac488b2a147b7a">LED2</a>, <a name="a3"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fa5dec293e081e0fc78369c842fab8452b">LED3</a>, <a name="a4"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fad60e39b8d1701d30aa64f80343217342">LED4</a>, <a name="a5"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fadc6f24fd6915a3f2786a1b7045406924">END</a>); <span class="comment">// debug leds</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define ledACT  leds[0]     // RF active led</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define ledCON  leds[1]     // Connected led</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define ledRXD  leds[2]     // RXD Event led</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define ledTXD  leds[3]     // TXD Ack   led</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// MyFirmataClass Object</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line">MyFirmataClass myFirmata;</div>
<div class="line"></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// my BLE class</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="keyword">class </span>myBLE: <span class="keyword">public</span> <a name="_a6"></a><a class="code" href="../../d7/d03/classble_serial.html">bleSerial</a> {</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Construct for myBLE class</span></div>
<div class="line">    <span class="comment">//     set the Vendor Name, Device Name, Device Version</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    myBLE(): <a class="code" href="../../d7/d03/classble_serial.html">bleSerial</a>(<span class="stringliteral">&quot;BLE-Multiple&quot;</span>) {</div>
<div class="line">        <span class="comment">// nothing</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Override the bleSerial::onConnected() event</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a7"></a><a class="code" href="../../d7/d03/classble_serial.html#adb930ce55295ff7eb6ab5bdbeb702749">onConnected</a>() {</div>
<div class="line">        <a class="code" href="../../d7/d03/classble_serial.html#adb930ce55295ff7eb6ab5bdbeb702749">bleSerial::onConnected</a>();</div>
<div class="line"></div>
<div class="line">        <span class="comment">// your connected event code here</span></div>
<div class="line">        ledCON = <a name="a8"></a><a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80add01b80eb93658fb4cf7eb9aceb89a1d">LED_ON</a>;    <span class="comment">// turn ON the Connected LED</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Override the bleSerial::onDisconnected() event</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a9"></a><a class="code" href="../../d7/d03/classble_serial.html#aa6f5116c8bcf05011a05ad5e2967c318">onDisconnected</a>() {</div>
<div class="line">        <a class="code" href="../../d7/d03/classble_serial.html#aa6f5116c8bcf05011a05ad5e2967c318">bleSerial::onDisconnected</a>();</div>
<div class="line"></div>
<div class="line">        <span class="comment">// your disconnect event code here</span></div>
<div class="line">        systemResetCallback();</div>
<div class="line">        ledCON = <a name="a10"></a><a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;   <span class="comment">// turn OFF all LEDS</span></div>
<div class="line">        ledRXD = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;</div>
<div class="line">        ledTXD = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Override the bleSerial::onWatchdog() event</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a11"></a><a class="code" href="../../d7/d03/classble_serial.html#a8af315789cc459a1fde96ac80ca9ffb7">onWatchdog</a>() {</div>
<div class="line">        <a class="code" href="../../d7/d03/classble_serial.html#a8af315789cc459a1fde96ac80ca9ffb7">bleSerial::onWatchdog</a>();</div>
<div class="line"></div>
<div class="line">        <span class="comment">// your watchdog event code here</span></div>
<div class="line">        systemResetCallback();</div>
<div class="line">        ledCON = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;   <span class="comment">// turn OFF all LEDS</span></div>
<div class="line">        ledRXD = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;</div>
<div class="line">        ledTXD = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Override the bleSerial::onError() event</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a12"></a><a class="code" href="../../d7/d03/classble_serial.html#a8a0622f5d10fe2242978a16b2d1e6ee4">onError</a>(<a class="code" href="../../db/dff/group___enumerations.html#ga6c00522f6a8c33135ee0414877d42c04">BLE_ERR_T</a> err, LPCTSTR <span class="keywordtype">id</span>) {</div>
<div class="line">        DBG(<span class="stringliteral">&quot;Error %d on %s\n&quot;</span>, err, <span class="keywordtype">id</span>);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Override the bleSerial::onBleRecv(...)</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> onBleRecv(uint8_t *buf, <span class="keywordtype">int</span> len) {</div>
<div class="line">        bleSerial::onBleRecv(buf, len);</div>
<div class="line"></div>
<div class="line">        <span class="comment">// your onBleRecv code here</span></div>
<div class="line">        ledRXD.invert();    <span class="comment">// to invert the RXD LED</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Override the bleSerial::onBleSend()</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> onBleSend(uint8_t ack) {</div>
<div class="line">        bleSerial::onBleSend(ack);</div>
<div class="line"></div>
<div class="line">        <span class="comment">// your onBleSend code here</span></div>
<div class="line">        ledTXD.invert();    <span class="comment">// to invert the TXD Ack LED</span></div>
<div class="line">    }</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// power Save Class</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="keyword">class </span>myPowerSave: <span class="keyword">public</span> <a name="_a13"></a><a class="code" href="../../d5/d3e/class_c_power_save.html">CPowerSave</a> {</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> onSleep(uint32_t ulExpectedIdleTime) {</div>
<div class="line">        ledACT = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;</div>
<div class="line">        CPowerSave::onSleep(ulExpectedIdleTime);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> onWakeup(uint32_t *pExpectedIdleTime) {</div>
<div class="line">        CPowerSave::onWakeup(pExpectedIdleTime);</div>
<div class="line">        ledACT = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80add01b80eb93658fb4cf7eb9aceb89a1d">LED_ON</a>;</div>
<div class="line">    }</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// myProximity Class</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="keyword">class </span>myProximity : <span class="keyword">public</span> <a name="_a14"></a><a class="code" href="../../de/d67/classble_proximity.html">bleProximity</a> {</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    myProximity(<a class="code" href="../../d7/d03/classble_serial.html">bleSerial</a> &amp;ble) :</div>
<div class="line">        <a class="code" href="../../de/d67/classble_proximity.html">bleProximity</a>(ble),</div>
<div class="line">        m_ledAlert(<a name="a15"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fab20c93c473d2b2ee6bef8e048791d3e2">P5</a>)</div>
<div class="line">    {</div>
<div class="line">        m_ledAlert.output();    <span class="comment">// set ledAlert as output pin</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a16"></a><a class="code" href="../../de/d67/classble_proximity.html#a43c9187ddcb7237099ff414b74c5b0bd">onAlert</a>(uint8_t level) {</div>
<div class="line">        DBG(<span class="stringliteral">&quot;On Alert: %d\n&quot;</span>, level);</div>
<div class="line">        <span class="keywordflow">switch</span>(level) {</div>
<div class="line">        <span class="keywordflow">case</span> 0:</div>
<div class="line">            m_ledAlert = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80afc0ca8cc6cbe215fd3f1ae6d40255b40">LED_OFF</a>;</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> 1:</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> 2:</div>
<div class="line">            m_ledAlert = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80add01b80eb93658fb4cf7eb9aceb89a1d">LED_ON</a>;</div>
<div class="line">            tone(<a name="a17"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fa61f13c350e3003e8df7d8001bb015b19">P19</a>, 3800, 150, 3);    <span class="comment">// freq=3800Hz, duration=150ms, count=3 times</span></div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        }</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> onLinkLose(uint8_t level) {</div>
<div class="line">        DBG(<span class="stringliteral">&quot;On Link Lose: %d\n&quot;</span>, level);</div>
<div class="line">    }</div>
<div class="line"><span class="keyword">protected</span>:</div>
<div class="line">    <a name="_a18"></a><a class="code" href="../../d7/db9/class_c_pin.html">CPin</a> m_ledAlert;</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// main loop</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">void</span>) {</div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">    #if __USE_USB</span></div>
<div class="line"><span class="preprocessor"></span>        <a name="_a19"></a><a class="code" href="../../d6/dc5/classusb_c_d_c.html">usbCDC</a> ser;</div>
<div class="line">        ser.<a name="a20"></a><a class="code" href="../../d6/dc5/classusb_c_d_c.html#a49d431de50da7ccacf91ef33b04e99d2">connect</a>();</div>
<div class="line"><span class="preprocessor">    #else</span></div>
<div class="line"><span class="preprocessor"></span>        <a name="_a21"></a><a class="code" href="../../d8/d1d/class_c_serial.html">CSerial</a> ser;</div>
<div class="line">        ser.<a name="a22"></a><a class="code" href="../../d8/d1d/class_c_serial.html#a0c80ea521e310305579e787f21a55233">settings</a>(115200);</div>
<div class="line"><span class="preprocessor">    #endif</span></div>
<div class="line"><span class="preprocessor"></span>    <a name="_a23"></a><a class="code" href="../../d4/d37/class_c_debug.html">CDebug</a> dbg(ser);</div>
<div class="line">    dbg.start();</div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">    #undef __USE_USB</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line">    <span class="comment">/*************************************************************************</span></div>
<div class="line"><span class="comment">     *</span></div>
<div class="line"><span class="comment">     *                         your setup code here</span></div>
<div class="line"><span class="comment">     *</span></div>
<div class="line"><span class="comment">     **************************************************************************/</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// BLE Engine (Serial Stream)</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    myBLE ble;</div>
<div class="line">    ble.advertising(100, -70, 8);   <span class="comment">// set adv. interval = 100ms, calibrater tx power = -70dBm, conn. interval=8ms</span></div>
<div class="line">    ble.enable();                   <span class="comment">// start the ble engine first!!</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Device Information Service</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <a name="_a24"></a><a class="code" href="../../d3/dc5/classble_device_info.html">bleDeviceInfo</a> info(ble);</div>
<div class="line">    info.setManufactureName(u8<span class="stringliteral">&quot;英倍達國際&quot;</span>); <span class="comment">// u8 mean to use the UTF-8 string</span></div>
<div class="line">    info.setModelNumber(<span class="stringliteral">&quot;nano11U37&quot;</span>);</div>
<div class="line">    info.setSerialNumber(<span class="stringliteral">&quot;140226000&quot;</span>);</div>
<div class="line">    info.setFirmwareRevision(uCXpresso_VER_STR);</div>
<div class="line">    info.setHardwareRevision(<span class="stringliteral">&quot;R1&quot;</span>);</div>
<div class="line">    info.setPnP(<a name="a25"></a><a class="code" href="../../db/dff/group___enumerations.html#gga42b352f4817787f82d2adcffd4793ad9a704d8d311290a7c789e3902af79ae043">VS_USB</a>, 1, 2, 0x3456);      <span class="comment">// vendor Id=1, product Id = 2,  product ver. = 0x3456</span></div>
<div class="line"></div>
<div class="line">    SYS_ID_T sysId = {</div>
<div class="line">        {0x00, 0x01, 0x02, 0x03, 0x04},     <span class="comment">// Manufacturer Identifier</span></div>
<div class="line">        {0x05, 0x06, 0x07}                  <span class="comment">// Organizationally Unique Identifier</span></div>
<div class="line">    };</div>
<div class="line">    info.setSystemId(sysId);</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Proximity Service</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    myProximity alert(ble); <span class="comment">// declare Proximity Service (Immediate alert + Lose Link)</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Battery Level Service</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <a name="_a26"></a><a class="code" href="../../d8/d3b/classble_battery_level.html">bleBatteryLevel</a> bl(ble);    <span class="comment">// declare Battery Level Service object</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Health Thermometer Service</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <a name="_a27"></a><a class="code" href="../../d9/d26/classble_health_thermometer.html">bleHealthThermometer</a> ht(ble);   <span class="comment">// declare Health Thermometer Service object</span></div>
<div class="line">    ht.measurementInterval(1);          <span class="comment">// set measurement interval = 1 second</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Arduino Firmata</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    myFirmata.begin(ble);               <span class="comment">// begin the Firmata Object with the ble serial stream.</span></div>
<div class="line">    callback_init();                    <span class="comment">// initialize the callback functions for myFirmata Object.</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// A key input for Alert (for Proximity)</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <a class="code" href="../../d7/db9/class_c_pin.html">CPin</a> keyAlert(<a name="a28"></a><a class="code" href="../../db/dff/group___enumerations.html#gga65a2241721e4acb573e0c3fe29ac432fafc188cab65e9ef22b16cffc65fc01db6">P8</a>);                  <span class="comment">// define P8 as a push button</span></div>
<div class="line">    keyAlert.input();</div>
<div class="line">    <a class="code" href="../../db/dff/group___enumerations.html#ga6f24594071a026b31238ab8cb80d6a80">PIN_LEVEL_T</a> pinVal = keyAlert;</div>
<div class="line"></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Timeout for time interval</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <a name="_a29"></a><a class="code" href="../../d5/d5b/class_c_timeout.html">CTimeout</a> t1, t2, t3;                <span class="comment">// t1=analog input check, t2=temperature check, t3=battery check</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifndef DEBUG</span></div>
<div class="line"><span class="preprocessor"></span>    <span class="comment">//</span></div>
<div class="line">    <span class="comment">// Power Save Feature</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    myPowerSave ps;                     <span class="comment">// use power Save feature</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line">    <span class="keywordtype">float</span>   value;</div>
<div class="line">    uint8_t level;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">while</span>(1) {</div>
<div class="line">        <span class="comment">/**********************************************************************</span></div>
<div class="line"><span class="comment">         *</span></div>
<div class="line"><span class="comment">         *                         your loop code here</span></div>
<div class="line"><span class="comment">         *</span></div>
<div class="line"><span class="comment">         **********************************************************************/</span></div>
<div class="line">        <span class="keywordflow">if</span> ( ble.isConnected() ) {</div>
<div class="line"><span class="preprocessor">#ifndef DEBUG</span></div>
<div class="line"><span class="preprocessor"></span>            <span class="comment">//</span></div>
<div class="line">            <span class="comment">// Power On</span></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            ps.disable(); <span class="comment">// disable power save mode</span></div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor"></span>            ledACT = !ledACT;</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span>            <span class="comment">//</span></div>
<div class="line">            <span class="comment">// UART Service</span></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            <span class="keywordflow">if</span> ( ble.isAvailable() ) {</div>
<div class="line"></div>
<div class="line">                <span class="comment">//</span></div>
<div class="line">                <span class="comment">// BLE Firmata Demo</span></div>
<div class="line">                <span class="comment">//</span></div>
<div class="line">                <span class="keywordflow">if</span> ( myFirmata.available() ) {</div>
<div class="line">                    <span class="keywordflow">do</span> {</div>
<div class="line">                        myFirmata.processInput();</div>
<div class="line">                    } <span class="keywordflow">while</span>(myFirmata.available());</div>
<div class="line">                } <span class="keywordflow">else</span> {</div>
<div class="line">                    <span class="comment">//</span></div>
<div class="line">                    <span class="comment">// check the Digital Input</span></div>
<div class="line">                    <span class="comment">//</span></div>
<div class="line">                    checkDigitalInputs();</div>
<div class="line"></div>
<div class="line">                    <span class="comment">//</span></div>
<div class="line">                    <span class="comment">// check the Analog Input with a sampling Interval</span></div>
<div class="line">                    <span class="comment">//</span></div>
<div class="line">                    <span class="keywordflow">if</span> (t1.<a name="a30"></a><a class="code" href="../../d5/d5b/class_c_timeout.html#a3841c7c53689d2ef8559a6c5e402aa5b">isExpired</a>(samplingInterval) ) {</div>
<div class="line">                        t1.<a name="a31"></a><a class="code" href="../../d5/d5b/class_c_timeout.html#a663a09bb51c1de8f8d83bd62fdc1a688">reset</a>();</div>
<div class="line">                        checkAnalogInputs();</div>
<div class="line">                    }</div>
<div class="line">                }</div>
<div class="line">            }</div>
<div class="line"></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            <span class="comment">// Proximity Service</span></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            <span class="keywordflow">if</span> ( alert.isAvailable() ) {</div>
<div class="line">                    <span class="comment">//</span></div>
<div class="line">                    <span class="comment">// push button check (Proximity Service)</span></div>
<div class="line">                    <span class="comment">//</span></div>
<div class="line">                    <span class="keywordflow">if</span> ( keyAlert != pinVal ) {</div>
<div class="line">                        pinVal = keyAlert;  <span class="comment">// keep last status of key</span></div>
<div class="line">                        alert.sendEvent(keyAlert.read()==<a name="a32"></a><a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80a6a226f4143ca3b18999551694cdb72a8">LOW</a> ? 1 : 0);</div>
<div class="line">                    }</div>
<div class="line">            }</div>
<div class="line"></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            <span class="comment">// Health Thermometer Service</span></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            <span class="keywordflow">if</span> ( ht.isAvailable() ) {</div>
<div class="line">                <span class="comment">//</span></div>
<div class="line">                <span class="comment">// check temperature</span></div>
<div class="line">                <span class="comment">//</span></div>
<div class="line">                <span class="keywordflow">if</span> ( t2.<a class="code" href="../../d5/d5b/class_c_timeout.html#a3841c7c53689d2ef8559a6c5e402aa5b">isExpired</a>(1000) ) {</div>
<div class="line">                    t2.<a class="code" href="../../d5/d5b/class_c_timeout.html#a663a09bb51c1de8f8d83bd62fdc1a688">reset</a>();</div>
<div class="line">                    <span class="keywordflow">if</span> ( ht.readTemperature(value) ) {</div>
<div class="line">                        ht.sendMeasure(value);</div>
<div class="line">                        DBG(<span class="stringliteral">&quot;temp=%0.2f\n&quot;</span>, value);</div>
<div class="line">                    } <span class="comment">// */</span></div>
<div class="line">                }</div>
<div class="line">            }</div>
<div class="line"></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            <span class="comment">// Battery Service</span></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            <span class="keywordflow">if</span> ( bl.isAvailable() ) {</div>
<div class="line">                <span class="comment">//</span></div>
<div class="line">                <span class="comment">// update Battery Level</span></div>
<div class="line">                <span class="comment">//</span></div>
<div class="line">                <span class="keywordflow">if</span> ( t3.<a class="code" href="../../d5/d5b/class_c_timeout.html#a3841c7c53689d2ef8559a6c5e402aa5b">isExpired</a>(3000) ) {</div>
<div class="line">                    t3.<a class="code" href="../../d5/d5b/class_c_timeout.html#a663a09bb51c1de8f8d83bd62fdc1a688">reset</a>();</div>
<div class="line">                    <span class="keywordflow">if</span> ( bl.readSystemVoltage(value) ) {</div>
<div class="line">                        <span class="keywordflow">if</span> ( value&gt;=2.4 &amp;&amp; value&lt;=3.6 ) {</div>
<div class="line">                            level =  map(value, 2.0, 3.3, 0, 100);</div>
<div class="line">                            bl.sendBatteryLevel(level);</div>
<div class="line">                            DBG(<span class="stringliteral">&quot;battery:%0.2fv %d%c\n&quot;</span>, value, level, <span class="charliteral">&#39;%&#39;</span>);</div>
<div class="line">                        }</div>
<div class="line">                    }</div>
<div class="line">                }</div>
<div class="line">            }</div>
<div class="line"></div>
<div class="line">        }   <span class="comment">// isConnected</span></div>
<div class="line">        <span class="keywordflow">else</span> {</div>
<div class="line"><span class="preprocessor">#ifndef DEBUG</span></div>
<div class="line"><span class="preprocessor"></span>            <span class="comment">//</span></div>
<div class="line">            <span class="comment">// Power Save</span></div>
<div class="line">            <span class="comment">//</span></div>
<div class="line">            ps.enable(<a name="a33"></a><a class="code" href="../../db/dff/group___enumerations.html#ggab0f874168f0e852de8b43f54553826d5a2abda45f1b1e5bbe193611fb41ae7dfa">POWER_DOWN</a>);  <span class="comment">// enable power save mode</span></div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor"></span>            ledACT = <a class="code" href="../../db/dff/group___enumerations.html#gga6f24594071a026b31238ab8cb80d6a80add01b80eb93658fb4cf7eb9aceb89a1d">LED_ON</a>;</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span>            sleep(1000);</div>
<div class="line">        }</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> 0 ;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// default memory pool</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="keyword">static</span> uint8_t mem_pool[DEFAULT_POOL_SIZE-480]; <span class="comment">// reduce pool size to increase the global stack</span></div>
<div class="line"></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// setup before the system startup</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span> <span class="keywordtype">void</span> sys_setup(<span class="keywordtype">void</span>) {</div>
<div class="line">    pool_memadd((uint32_t)mem_pool, <span class="keyword">sizeof</span>(mem_pool));</div>
<div class="line"><span class="preprocessor">#if __USE_USB==0</span></div>
<div class="line"><span class="preprocessor"></span>    pool_memadd(USB_MEM_BASE, USB_MEM_SIZE);</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span>}</div>
<div class="line"></div>
</div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Fri Apr 11 2014 23:43:25 for uCXpresso.BLE by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.6 </li>
  </ul>
</div>
</body>
</html>
